<?php

namespace common\models\shop;

use Yii;
use common\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%shop_type_spec}}".
 *
 * @property int $id
 * @property int $type_id 分类ID
 * @property int $spec_id 规格ID
 */
class ShopTypeSpec extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_type_spec}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['type_id', 'spec_id'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'type_id' => '分类ID',
            'spec_id' => '规格ID',
        ];
    }

    public function getSpec() {
        return $this->hasOne(ShopSpec::class, ['id' => 'spec_id']);
    }

    public function getValues() {
        return $this->hasMany(ShopSpecValue::class, ['spec_id' => 'spec_id']);
    }

    public function getSpecName() {
        return $this->spec ? $this->spec->name : '';
    }

    public function getSpecValues() {
        $values = $this->values;
        if (!empty($values)) {
            return ArrayHelper::map($values, 'id', 'value');
        }
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'type_id',
            'spec_id',
            'spec_name' => 'specName',
            'spec_values' => 'specValues',
        ];
    }

}
